/*!
 * 首页，获得当前用户信息，加载菜单
 */
jQuery(function($) {
	getLoginUser();
	loadMenu();
	$(window).resize(function() {
		resizeContent();
	});
	resizeContent();
});
function resizeContent() {
	var h = $(window).height() - 91;
	$(".page-content").css("height", h + "px");
}
function getLoginUser() {
	$.ajax({
		url: prefix + '/getLoginUser',
		type:'post',
		data:{},
		dataType:'json',
		async : true,
		error:function(){
			//alert('获取登录用户失败');
			window.location = prefix + '/index/login';
		},
		success:function(data){
			if (data.success) {
				$('.user-info').html('<small>欢迎，</small> ' + data.data);
			} else {
				//alert('获取登录用户失败');
				window.location = prefix + '/index/login';
			}
		}
	});
}

function loadMenu() {
	$.ajax({
		url: prefix + "/sys/menu/menuTree",
		type:'post',
		data:{},
		dataType:'json',
		async : true,
		error:function(){
			
		},
		success:function(data){
			getMenu(data);
		}
	});
}

function getMenu(data) {
	var menu = $("#AppMenu");
	for (var i=0; i<data.length; i++) {
		var m = data[i];
		var ul = getSubMenu(m.subMenus);
		var h = '<li id="menu' + m.menuId + '" class=""><a href="'
			+ (ul == '' ? prefix+m.url : '#" class="dropdown-toggle') + '"><i class="menu-icon fa '
			+ (m.icon=='' ? 'fa-folder-o' : m.icon) + '"></i><span class="menu-text"> ' + m.menuName + ' </span>'
			+ (ul == '' ? '' : '<b class="arrow fa fa-angle-down"></b>') + '</a><b class="arrow"></b>' + ul + '</li>';
		menu.append(h);
	}
}
function getSubMenu(subs) {
	var ret = '';
	for (var i=0; i<subs.length; i++) {
		var m = subs[i];
		var ul = getSubMenu(m.subMenus);
		ret = ret + '<li id="menu' + m.menuId + '" class=""><a href="'
			+ (ul == '' ? prefix +m.url : '#" class="dropdown-toggle') + '"><i class="menu-icon fa '
			+ (m.icon == '' ? 'fa-caret-right' : m.icon) + '"></i> ' + m.menuName
			+ (ul == '' ? '' : '<b class="arrow fa fa-angle-down"></b>') + ' </a><b class="arrow"></b>' + ul + '</li>';
	}
	if (ret != '') ret = '<ul class="submenu">' + ret + '</ul>';
	return ret;
}

function openMenuUrl(m) {
	$("#AppMenu li").removeClass("active");
	var h = "";
	var n = 0;
	var o = m;
	while(n < 10){
		o = o.parent().closest("li");
		if (o.length == 0) {
			break;
		}
		o.addClass("active");
		h = '<li>' + o.children(':first').text() + '</li>' + h;
		n++;
	}
	$("#breadcrumbs > ul").html('<li><i class="ace-icon fa fa-home home-icon"></i><a href="#" onclick="openMainPage()">首页</a></li>' + h);
	var f = '<iframe class="main_iframe" id="iframe1" name="iframe1" style="width:100%;height:100%;" src="' + m.attr("href") + '" frameborder="0"></iframe>';
    $('.page-content').html('');
    $('.page-content').append(f);

}

function openMainPage() {
	$("#breadcrumbs > ul").html('<li><i class="ace-icon fa fa-home home-icon"></i><a href="#" onclick="openMainPage()">首页</a></li>');
	var f = '<iframe class="main_iframe" id="iframe1" name="iframe1" style="width:100%;height:100%;" src="assets/blank.html" frameborder="0"></iframe>';
    $('.page-content').html('');
    $('.page-content').append(f);

}

function editPassword(){
    $("#breadcrumbs > ul").html('<li><i class="ace-icon fa fa-home home-icon"></i><a href="#" onclick="openMainPage()">首页</a></li>');
    var f = '<iframe class="main_iframe" id="iframe1" name="iframe1" style="width:100%;height:100%;" src="' +prefix + '/sys/user/userPassword" frameborder="0"></iframe>';
    $('.page-content').html('');
    $('.page-content').append(f);
}

function editUser(){
    alert("该功能暂未开放");
}

function logout() {
	$.ajax({
		url: prefix + "/logout",
		type:'post',
		data:{},
		dataType:'json',
		async : true,
		error:function(){

		},
		success:function(data){
			window.location = prefix + "/index/login";
		}
	});
}
